package cn.bdqn.controller.contract;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mysql.jdbc.StringUtils;

import cn.bdqn.pojo.Contract;
import cn.bdqn.service.contract.ContractService;
import cn.bdqn.tools.Constants;
import cn.bdqn.tools.PageSupport;
@Controller
public class ContractController {
					@Resource
					private ContractService contractService;
					private Contract contract=null;
					
					
			@RequestMapping("/contractlist")
			public String getContractList(Model model,HttpServletRequest rq){
				String pageIndex=rq.getParameter("pageIndex");
				String contract_number=rq.getParameter("contract_number");
				if(StringUtils.isNullOrEmpty(contract_number)){
					contract_number="";
				}
				if(pageIndex ==null){
					pageIndex="1";
				}
				int pageCurNo=Integer.parseInt(pageIndex);
				int getcount=contractService.getCount();
				PageSupport ps=new PageSupport();
				ps.setPageSize(Constants.pageSize);
				ps.setCurrentPageNo(pageCurNo);
				ps.setTotalCount(getcount);
				int totalPageCount=ps.getTotalPageCount();
				List<Contract> list=contractService.getContractList(contract_number,(pageCurNo-1)*Constants.pageSize, Constants.pageSize);
				model.addAttribute("contractlist",list);
				model.addAttribute("pageIndex",pageCurNo);
				model.addAttribute("count",getcount);
				model.addAttribute("contract_number",contract_number);
				model.addAttribute("totalPageCount",totalPageCount);
				return "contractlist";
			}
					
			@RequestMapping("/updateContract")
			public String selectContractList(String id,Model model,@ModelAttribute("contact")Contract contract){
				contract=contractService.selectContractList(Integer.parseInt(id));
				model.addAttribute("contract",contract);
				return "contractmodify";
			}
			
			@RequestMapping("/contractupdate")
			public String updateContractList(Contract contract){ 
				int i=contractService.updateContractList(contract);
				if(i>0){
					return "redirect:/contractlist";
				}else{
					return "error";
				}	
			}
			@RequestMapping("/add")
			public String add(){
				return "contractadd";
			}
			
			@RequestMapping("/contractaddlist")
			public String addContractList(HttpServletRequest rq,Contract contract){
				int i=contractService.addContractList(contract);
				if(i>0){
					return "redirect:/contractlist";
				}else{
					return "error";
				}	
			}
			
			@RequestMapping("/delete")
			public String deletyById(String id){
				int i=contractService.deleteById(Integer.parseInt(id));
				if(i>0){
					return "redirect:/contractlist";
				}else{
					return "error";
				}
			}
			
			@RequestMapping("/selectById")
			public String selectById(String id,Model model){
				contract=contractService.selectById(Integer.parseInt(id));
				model.addAttribute("contract",contract);
				return "contractview";
			}
			
			
			
					
}
